<template>
    <div class="searchTop" >
        <div class="searchTopNav">
            <div class="letf">
                <svg class="icon" aria-hidden="true" @click="$router.go(-1)">
                    <use xlink:href="#icon-zuo1"></use>
                </svg>
            </div>
            <div class="right" @click="reSou">
              <input type="text" v-model="searchKeyword" :placeholder="placeholder" @keydown.enter="saveKeyWord">
            </div>
            <svg class="icon" aria-hidden="true" @click="saveKeyWord">
                <use xlink:href="#icon-sousuo"></use>
            </svg>
        </div>
        <div class="reShou" v-if="hotList.length>0" v-show="is"><p v-for="(item,i) in hotList" :key="i" @click="setModel(item.first)">{{item.first}}</p></div>
        <div class="history" v-show="searchSongs.length==0||searchKeyword==''">
            <div class="historyLeft">
                历史
            </div>
            <div class="historyRight">
                <div @click="hisrorySearch(item)" class="keywordItem" v-for="(item,i) in keywordList" :key="i">
                    {{item}}
                </div>
            </div>
        </div>
        <div class="dle" @click="dle" v-show="searchSongs.length==0||searchKeyword==''">清空历史记录</div>
    </div>
      <div class="searchSongs" v-show="searchSongs.length!=0&&searchKeyword!=''">
        <div class="searchSongs-top">
            <div class="left">
                <svg class="icon" aria-hidden="true">
                    <use xlink:href="#icon-bofang3"></use>
                </svg>
            <div class="title">播放全部<span>(共{{searchSongs.length}}首)</span></div>
            </div>
        </div>
        <div class="list" >
            <div class="playitem" v-for="(item,i) in searchSongs" :key="i" @click="setplay(item,i)">
                <div class="left">
                    <div class="index">{{i+1}}</div>
                    <div class="content">
                        <div class="title">{{item.name}}</div>
                        <div>
                            <span class="tag" v-for="(tag,tags) in searchSongs.tags" :key="tags">{{tag}}&#8197;</span>
                            <span class="anthor" v-for="(list,index) in item.ar" :key="index">{{list.name}}&#8197;</span>
                        </div>
                    </div>
                </div>
                <div class="right">
                    <svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-bofang4"></use>
                    </svg>
                    <svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-liebiao1"></use>
                    </svg>
                </div>
            </div>
        </div>
    </div>
</template>
<style lang="less" scoped>
.searchTop{
    width: 7.5rem;
    padding: 0 0.4rem;
    height: calc(100vh-1.2rem);
    .searchTopNav{
        display: flex;
        width: 100%;
        height: 1.2rem;
        padding: 0 0.4rem;
        align-items: center;
        position: fixed;
        top: 0;
        left: 0;
        .icon{
            width: 0.5rem;
            height: 0.5rem;
        }
        .right{
            padding: 0 0 0 0.3rem;
            input{
                outline: none;
                border: none;
                border-bottom: 1px solid #666;
                width: 5.5rem;
                font-size: 0.3rem;
                height: 0.5rem;
            }
        }
    }
    .reShou{
        width: 5.5rem;
        font-size: 0.3rem;
        height: 5.2rem;
        border: 1px solid #ccc;
        background-color: white;
        position:absolute;
        top: 1.2rem;
        z-index: 888;
         p{
            margin: 0.1rem 0.2rem;
        }

    }
    .history{
        display: flex;
        margin-top: 1.2rem;
        .historyLeft{
            width: 0.8rem;
            height: 0.8rem;
            font-weight: 900;
            font-size: 0.35rem;
            line-height: 0.8rem;
        }
        .historyRight{
            color: #666;
            display: flex;
            flex-wrap: wrap;
            flex: 1;
            margin-left: 0.3rem;
            .keywordItem{
                background-color: #eee;
                height: 0.5rem;
                padding: 0 0.1rem;
                border-radius: 0.25rem;
                line-height: 0.5rem;
                 margin: 0.1rem 0.1rem;
            }
        }
    }
    .dle{
        float: right;
        font-weight: 900;
        color: #ccc;
    }
}

.searchSongs{
    width: 7.5rem;
    padding: 0 0.4rem;
    margin-top: 1.2rem;
    .searchSongs-top{
        display: flex;
        justify-content: space-between;
        height: 1rem;
        align-items: center;
        .left{
            align-items: center;
            display: flex;
            .icon{
                width: 0.6rem;
                height: 0.6rem;
            }
            .title{
                font-size: 0.4rem;
                font-weight: 900;
                margin-left: 0.2rem;
                span{
                    font-size: 0.3rem;
                    color: #ccc;
                }
            }
        }
    }
    .list{
        position: fixed;
        top: 2.2rem;
        bottom: 1.2rem;
        left: 0;
        overflow: scroll;
        width: 7.5rem;
        padding: 0 0.4rem;
         .playitem{
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 1rem;
            .left{
                display: flex;
                align-items: center;
                color: #666;
                .index{
                    width: 0.4rem;
                }
                .content{
                    .title{
                        font-size: 0.3rem;
                        font-weight: 900;
                        width: 4rem;
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                    }
                    margin-left: 0.4rem;
                    .tag{
                        font-size: 0.2rem;
                        color:orangered;
                        border: 1px solid orangered;
                        margin-right: 0.1rem;
                    }
                    .anthor{
                        color: #ccc;
                        font-size: 0.15rem;
                    }
                }
            }
            .right{
                .icon{
                    margin:0 0.1rem;
                }
            }
        }
    }
}
</style>
<script>
import { searchMusic, setHot } from '@/api/index.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      placeholder: '请输入歌名/歌手名',
      keywordList: [],
      searchKeyword: '',
      searchSongs: [],
      hotList: [],
      is: false
    }
  },
  beforeMount () {
    if (localStorage.keywordList != null) {
      this.keywordList = localStorage.keywordList ? JSON.parse(localStorage.keywordList) : []
    }
  },
  methods: {
    saveKeyWord: async function () {
      if (this.searchKeyword !== '') {
        if (this.keywordList.indexOf(this.searchKeyword) === -1) {
          this.keywordList.push(this.searchKeyword)
        }
        this.keywordList = Array.from(new Set(this.keywordList))
        if (this.keywordList.length > 10) {
          this.keywordList = this.keywordList.slice(this.keywordList.length - 10, this.keywordList.length)
        }
        localStorage.keywordList = JSON.stringify(this.keywordList)
        const result = await searchMusic(this.searchKeyword)
        this.searchSongs = result.data.result.songs
        //    console.log(this.searchSongs)
      } else {
        Toast({
          message: '请输入关键词!',
          position: 'top'
        })
      }
    },
    hisrorySearch (keyword) {
      this.searchKeyword = keyword
      this.saveKeyWord()
    },
    setplay (item, i) {
      this.$store.commit('pushPlaylist', item)
      this.$store.commit('setPlayIndex', this.$store.state.playlist.length - 1)
    },
    dle () {
      this.keywordList = []
    },
    async reSou () {
      if (this.searchKeyword === '' && this.is === false) {
        this.is = true
        const hot = await setHot()
        this.hotList = hot.data.result.hots
        console.log(this.hotList)
      }
    },
    setModel (item) {
      this.searchKeyword = item
      this.is = false
    }
  }
}
</script>
